package com.ytjj.qmyx.supplychain.service;

import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.exception.ApiException;
import com.ytjj.qmyx.supplychain.common.model.request.BatchProductLabelRequest;
import com.ytjj.qmyx.supplychain.mapper.ProductLabelMapper;
import com.ytjj.qmyx.supplychain.mapper.ProductLabelProductMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductLabel;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductLabelProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@Service
public class ProductLabelService {
    @Resource
    private ProductLabelMapper productLabelMapper;
    @Resource
    private ProductLabelProductMapper productLabelProductMapper;

    public CommonResult addAndUpdate(String productLabelName, Integer productLabelId, Integer adminId, String colour) {
        Example productLabelExample = new Example(YxProductLabel.class);
        if (null == productLabelId) {
            productLabelExample.createCriteria()
                    .andEqualTo("productLabelName", productLabelName);
            long count = productLabelMapper.selectCountByExample(productLabelExample);
            if (count > 0) {
                throw new ApiException("该标签名已存在");
            }
            YxProductLabel productLabel = new YxProductLabel();
            productLabel.setProductLabelName(productLabelName);
            productLabel.setCreateTime(new Date());
            productLabel.setAdminId(adminId);
            productLabel.setColour(colour);
            int i = productLabelMapper.insertSelective(productLabel);
            if (i > 0) {
                return CommonResult.success(i,"添加标签成功");
            } else {
                return CommonResult.failed("添加标签失败");
            }
        } else {
            productLabelExample.createCriteria().andEqualTo("productLabelName", productLabelName)
                                                .andNotEqualTo("productLabelId", productLabelId);
            long count = productLabelMapper.selectCountByExample(productLabelExample);
            if (count > 0) {
                throw new ApiException("该标签名已存在");
            }
            YxProductLabel productLabel = new YxProductLabel();
            productLabel.setProductLabelName(productLabelName);
            productLabel.setAdminId(adminId);
            productLabel.setProductLabelId(productLabelId);
            productLabel.setColour(colour);
            int i = productLabelMapper.updateByPrimaryKeySelective(productLabel);
            if (i > 0) {
                return CommonResult.success(i,"修改标签成功");
            } else {
                return CommonResult.failed("修改标签失败");
            }
        }
    }

    public CommonResult delete(Integer productLabelId) {
        Example productProductLabelExample = new Example(YxProductLabelProduct.class);
        productProductLabelExample.createCriteria().andEqualTo("productLabelId", productLabelId);
        List<YxProductLabelProduct> yxProductLabelProducts = productLabelProductMapper.selectByExample(productProductLabelExample);
        int count = 1;
        if (yxProductLabelProducts.size() > 0) {
            count = productLabelProductMapper.deleteByExample(productProductLabelExample);
        }
        int i = productLabelMapper.deleteByPrimaryKey(productLabelId);
        if (i > 0) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.failed("删除失败");
        }
    }

    public long userNumBylabelId(Integer productLabelId) {
        Example productProductLabelExample = new Example(YxProductLabel.class);
        productProductLabelExample.createCriteria().andEqualTo("productLabelId", productLabelId);
        return productLabelProductMapper.selectCountByExample(productProductLabelExample);
    }

    public List<YxProductLabel> selectAllLabel() {
        List<YxProductLabel> yxProductLabels = productLabelMapper.selectAll();
        return yxProductLabels;
    }

    public CommonResult batchAddProductLabel(BatchProductLabelRequest request) {
        AtomicReference<Integer> count = new AtomicReference<>(0);
        List<Integer> productIds = request.getProductIds();
        List<Integer> productLabelIds = request.getProductLabelIds();
        productIds.stream().forEach(productId ->{
            Example productProductLabelExample = new Example(YxProductLabelProduct.class);
            productProductLabelExample.createCriteria().andEqualTo("productId",productId);
            productLabelProductMapper.deleteByExample(productProductLabelExample);
            productLabelIds.stream().forEach(productLabelId ->{
                YxProductLabelProduct productProductLabel = new YxProductLabelProduct();
                productProductLabel.setAdminId(request.getAdminId());
                productProductLabel.setProductId(productId);
                productProductLabel.setProductLabelId(productLabelId);
                productProductLabel.setCreateTime(new Date());
                productLabelProductMapper.insertSelective(productProductLabel);
                count.getAndSet(count.get() + 1);
            });
        });
        return CommonResult.success(count.get());
    }

    public CommonResult batchDeleteProductLabel(BatchProductLabelRequest request) {
        Example example = new Example(YxProductLabelProduct.class);
        example.createCriteria().andIn("productId",request.getProductIds());
        productLabelProductMapper.deleteByExample(example);
        return CommonResult.success();
    }
}
